﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;
using CrystalDecisions.CrystalReports.Engine;

//开发者:史麒亮
namespace LCWMS
{
    public partial class w_asn998 : DockContent
    {
        private DataSet newdscustomer;
        private DataSet dscustomer = new DataSet();
        private DataSet ds_temp = new DataSet();
        private DataSet i_ds_send_detail = new DataSet();
        private int i_dist = 0;//界面状态，0--没操作;1--新增;2--修改;3--删除。
        private int i_rows_count = 0;
        private string i_sql_send_detail;
        private BarcodeLib.Barcode b_bar = new BarcodeLib.Barcode();
        private bool b_temp = true;
        public w_asn998()
        {
            InitializeComponent();
            dgv_send_info.AutoGenerateColumns = false;
            dgv_send_detail.AutoGenerateColumns = false;
        }

        #region 自定义函数
        /// <summary>
        /// panel_send_info内部控件状态调整
        /// </summary>
        /// <param name="b"></param>
        private void f_panel_send_info_dist(bool b)
        {
            tb_id_no_info.ReadOnly = !b;
            cbx_newcustomer.Enabled = b;
            tb_send_no_info.ReadOnly = !b;
            dtp_arrive_time.Enabled = b;
            tb_sku_item.ReadOnly = !b;

            cbx_rec_type_info.Enabled = b;
            tb_quantity.ReadOnly = !b;
            tb_net_weight.ReadOnly = !b;

            tb_send_name_info.ReadOnly = !b;
            cbx_send_type_info.Enabled = b;

            dtp_unload_time_s.Enabled = b;
            dtp_unload_time_e.Enabled = b;
            tb_unload_signer.ReadOnly = !b;

            dtp_check_time_s.Enabled = b;
            dtp_check_time_e.Enabled = b;
            tb_check_signer.ReadOnly = !b;

            tb_check_memo.ReadOnly = !b;

            dtp_sap_rec_s.Enabled = b;
            dtp_sap_rec_e.Enabled = b;
            tb_sap_signer.ReadOnly = !b;

            dtp_rec_pack_s.Enabled = b;
            dtp_rec_pack_e.Enabled = b;
            tb_rec_pack_signer.ReadOnly = !b;

            dtp_putaway_time_s.Enabled = b;
            dtp_putaway_time_e.Enabled = b;
            tb_putaway_signer.ReadOnly = !b;

            dtp_confirm_time_s.Enabled = b;
            dtp_confirm_time_e.Enabled = b;
            tb_confirm_signer.ReadOnly = !b;

            tb_memo.ReadOnly = !b; 


        }

        /// <summary>
        /// 显示明细
        /// </summary>
        /// <param name="dt_info"></param>
        /// <param name="i_row"></param>
        private void f_display_info(DataTable dt_info, int i_row)
        {
            //删除后默认第一行
            if (i_dist == 3)
            {
                i_row = 0;
                i_dist = 0;
            }


            tb_id_no_info.Text = dt_info.Rows[i_row]["id_no"].ToString().Trim();
            cbx_newcustomer.Text = dt_info.Rows[i_row]["consignor"].ToString().Trim();
            tb_send_no_info.Text = dt_info.Rows[i_row]["send_no"].ToString().Trim();
            dtp_arrive_time.Text = dt_info.Rows[i_row]["arrive_time"].ToString().Trim();
            tb_sku_item.Text = dt_info.Rows[i_row]["sku_item"].ToString().Trim();

            cbx_rec_type_info.Text = dt_info.Rows[i_row]["rec_type"].ToString().Trim();
            tb_quantity.Text = dt_info.Rows[i_row]["quantity"].ToString().Trim();
            tb_net_weight.Text = dt_info.Rows[i_row]["net_weight"].ToString().Trim();

            tb_send_name_info.Text = dt_info.Rows[i_row]["send_name"].ToString().Trim();
            cbx_send_type_info.Text = dt_info.Rows[i_row]["send_type"].ToString().Trim();

            dtp_unload_time_s.Text = dt_info.Rows[i_row]["unload_time_s"].ToString().Trim();
            dtp_unload_time_e.Text = dt_info.Rows[i_row]["unload_time_e"].ToString().Trim();
            tb_unload_signer.Text = dt_info.Rows[i_row]["unload_signer"].ToString().Trim();

            dtp_check_time_s.Text = dt_info.Rows[i_row]["check_time_s"].ToString().Trim();
            dtp_check_time_e.Text = dt_info.Rows[i_row]["check_time_e"].ToString().Trim();
            tb_check_signer.Text = dt_info.Rows[i_row]["check_signer"].ToString().Trim();

            tb_check_memo.Text = dt_info.Rows[i_row]["check_memo"].ToString().Trim();

            dtp_sap_rec_s.Text = dt_info.Rows[i_row]["sap_rec_s"].ToString().Trim();
            dtp_sap_rec_e.Text = dt_info.Rows[i_row]["sap_rec_e"].ToString().Trim();
            tb_sap_signer.Text = dt_info.Rows[i_row]["sap_signer"].ToString().Trim();

            dtp_rec_pack_s.Text = dt_info.Rows[i_row]["rec_pack_s"].ToString().Trim();
            dtp_rec_pack_e.Text = dt_info.Rows[i_row]["rec_pack_e"].ToString().Trim();
            tb_rec_pack_signer.Text = dt_info.Rows[i_row]["rec_pack_signer"].ToString().Trim();

            dtp_putaway_time_s.Text = dt_info.Rows[i_row]["putaway_time_s"].ToString().Trim();
            dtp_putaway_time_e.Text = dt_info.Rows[i_row]["putaway_time_e"].ToString().Trim();
            tb_putaway_signer.Text = dt_info.Rows[i_row]["putaway_signer"].ToString().Trim();

            dtp_confirm_time_s.Text = dt_info.Rows[i_row]["confirm_time_s"].ToString().Trim();
            dtp_confirm_time_e.Text = dt_info.Rows[i_row]["confirm_time_e"].ToString().Trim();
            tb_confirm_signer.Text = dt_info.Rows[i_row]["confirm_signer"].ToString().Trim();

            tb_memo.Text = dt_info.Rows[i_row]["memo"].ToString().Trim();
        }


        /// <summary>
        /// 清空明细
        /// </summary>
        private void f_clear_info()
        {

            tb_id_no_info.Text = "";

            tb_send_no_info.Text = "";
            dtp_arrive_time.Value = DateTime.Now;
            tb_sku_item.Text = "";

            cbx_rec_type_info.Text = "";
            tb_quantity.Text = "";
            tb_net_weight.Text = "";

            tb_send_name_info.Text = "";
            cbx_send_type_info.Text = "";

            dtp_unload_time_s.Value = DateTime.Now;
            dtp_unload_time_e.Value = DateTime.Now;
            tb_unload_signer.Text = "";

            dtp_check_time_s.Value = DateTime.Now;
            dtp_check_time_e.Value = DateTime.Now;
            tb_check_signer.Text = "";

            tb_check_memo.Text = "";

            dtp_sap_rec_s.Value = DateTime.Now;
            dtp_sap_rec_e.Value = DateTime.Now;
            tb_sap_signer.Text = "";

            dtp_rec_pack_s.Value = DateTime.Now;
            dtp_rec_pack_e.Value = DateTime.Now;
            tb_rec_pack_signer.Text = "";

            dtp_putaway_time_s.Value = DateTime.Now;
            dtp_putaway_time_e.Value = DateTime.Now;
            tb_putaway_signer.Text = "";

            dtp_confirm_time_s.Value = DateTime.Now;
            dtp_confirm_time_e.Value = DateTime.Now;
            tb_confirm_signer.Text = "";

            tb_memo.Text = "";
        }

        /// <summary>
        /// 绑定so_send_detail
        /// </summary>
        /// <param name="consignor"></param>
        /// <param name="id_no"></param>
        /// <param name="send_no"></param>
        private void f_Bind_so_send_detail(string consignor, string id_no, string send_no)
        {
            i_ds_send_detail = new DataSet();
            //查询语句
            i_sql_send_detail = "select * from " + main.g_space + ".so_send_detail where inv_org='" + main.g_org + "'";

            //拼接条件
            if (!string.IsNullOrEmpty(consignor))
            {
                i_sql_send_detail += " and consignor='" + consignor + "'";
            }

            if (!string.IsNullOrEmpty(id_no))
            {
                i_sql_send_detail += " and id_no='" + id_no + "'";
            }

            if (!string.IsNullOrEmpty(send_no))
            {
                i_sql_send_detail += " and send_no='" + send_no + "'";
            }

            try
            {
                if (main.dbtype == "SQL")
                {
                    i_ds_send_detail = main.dbservice.Sql_exec_selectsql(i_sql_send_detail);
                }
                else if (main.dbtype == "ORACLE")
                {
                    i_ds_send_detail = main.dbservice.oracle_exec_selectsql(i_sql_send_detail);
                }

                dgv_send_detail.DataSource = i_ds_send_detail.Tables[0];
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
        }

        /// <summary>
        /// 得到表头
        /// </summary>
        /// <returns></returns>
        private DataSet f_get_so_send_info_header()
        {
            //查询语句
            string s_sql;
            s_sql = "select * from " + main.g_space + ".so_send_info where inv_org='" + main.g_org + "' and main_dist <> 'D'";

            #region 拼接查询条件

            //货主
            //判断是否有货主
            if (cbx_customer.Text.Trim() != "")
            {
                s_sql +=  " and consignor = '" + cbx_customer.Text.Trim() + "' ";
            }

            //流水号
            if (!string.IsNullOrEmpty(tb_id_no.Text))
            {
                s_sql += " and id_no like '%" + tb_id_no.Text + "%'";
            }

            //运单号
            if (!string.IsNullOrEmpty(tb_send_no.Text))
            {
                s_sql += " and send_no like '%" + tb_send_no.Text + "%'";
            }

            //订单号
            if (!string.IsNullOrEmpty(tb_asn_no.Text))
            {
                s_sql += " and exists (select * from wms.so_send_detail b where WMS.so_send_info.inv_org=b.inv_org and WMS.so_send_info.id_no =b.id_no and WMS.so_send_info.consignor = b.consignor and WMS.so_send_info.send_no =b.send_no and asn_no like '%" + tb_asn_no.Text+ "%')";
            }

            //发货方
            if (!string.IsNullOrEmpty(tb_send_name.Text))
            {
                s_sql += " and send_name like '%" + tb_send_name.Text + "%'";
            }

            //收货类型
            if (!string.IsNullOrEmpty(cbx_rec_type.Text))
            {
                s_sql += " and rec_type = '" + cbx_rec_type.Text + "'";
            }

            //发货方式
            if (!string.IsNullOrEmpty(cbx_send_type.Text))
            {
                s_sql += " and send_type = '" + cbx_send_type.Text + "'";
            }

            if (main.dbtype == "SQL")
            {
                //到货时间
                s_sql += " and arrive_time between '" + dtp_begin.Value.ToString("yyyy.MM.dd") + "' and '" + Convert.ToDateTime(dtp_end.Value.ToString("yyyy.MM.dd")).AddDays(1).ToString("yyyy.MM.dd").Trim() + "'";
            }
            else if (main.dbtype == "ORACLE")
            {
                //到货时间
                s_sql += " and arrive_time between to_date('" + dtp_begin.Value.ToString("yyyy.MM.dd") + "','yyyy.mm.dd') and to_date('" + Convert.ToDateTime(dtp_end.Value.ToString("yyyy.MM.dd")).AddDays(1).ToString("yyyy.MM.dd").Trim() + "','yyyy.mm.dd')";
            }
           
            //维护区分
            s_sql += " and main_dist<>'D'";

            //排序
            s_sql += " order by made_time";

            #endregion
            DataSet ds = new DataSet();
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds = main.dbservice.Sql_exec_selectsql(s_sql);
                }
                else if (main.dbtype == "ORACLE")
                {
                    ds = main.dbservice.oracle_exec_selectsql(s_sql);
                }
                return ds;
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                return null;
            }
        }
        #endregion

        private void w_asn998_Load(object sender, EventArgs e)
        {
            
            DataSet f_ds = new DataSet();

            //初始化货主编码 
            try
            {
                dscustomer = main.g_class.comm_get_consignor();
                newdscustomer = dscustomer.Copy();
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";
                DataRow drnew;
                drnew = dscustomer.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                dscustomer.Tables[0].Rows.Add(drnew);
                cbx_customer.DataSource = dscustomer.Tables[0];
                if (dscustomer.Tables[0].Rows.Count > 0) tb_cusname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();
                cbx_newcustomer.ValueMember = "consignor";
                cbx_newcustomer.DisplayMember = "consignor";
                cbx_newcustomer.DataSource = newdscustomer.Tables[0];
                cbx_newcustomer.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
           

            try
            {
                //日期格式
                string custFormat = "yyyy-MM-dd HH:mm:ss";
                dtp_unload_time_s.CustomFormat = custFormat;
                dtp_unload_time_e.CustomFormat = custFormat;
                dtp_arrive_time.CustomFormat = custFormat;
                dtp_check_time_s.CustomFormat = custFormat;
                dtp_check_time_e.CustomFormat = custFormat;
                dtp_sap_rec_s.CustomFormat = custFormat;
                dtp_sap_rec_e.CustomFormat = custFormat;
                dtp_rec_pack_s.CustomFormat = custFormat;
                dtp_rec_pack_e.CustomFormat = custFormat;
                dtp_putaway_time_s.CustomFormat = custFormat;
                dtp_putaway_time_e.CustomFormat = custFormat;
                dtp_confirm_time_s.CustomFormat = custFormat;
                dtp_confirm_time_e.CustomFormat = custFormat;
                f_panel_send_info_dist(false);
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }

            //初始化收货类型
            try
            {

                f_ds = main.g_class.comm_get_dictionary("rec_type");
                DataSet newf_ds = f_ds;
                DataRow drnew;
                drnew = f_ds.Tables[0].NewRow();
                drnew["rec_type"] = "";
                f_ds.Tables[0].Rows.Add(drnew);
                cbx_rec_type.ValueMember = "rec_type";
                cbx_rec_type.DisplayMember = "rec_type";
                cbx_rec_type.DataSource = f_ds.Tables[0];
                cbx_rec_type.Text = "";

                DataSet f_ds_rec_type = main.g_class.comm_get_dictionary("rec_type");
                DataRow drnew_rec_type;
                drnew_rec_type = f_ds_rec_type.Tables[0].NewRow();
                drnew_rec_type["rec_type"] = "";
                f_ds_rec_type.Tables[0].Rows.Add(drnew_rec_type);
                cbx_rec_type_info.ValueMember = "rec_type";
                cbx_rec_type_info.DisplayMember = "rec_type";
                cbx_rec_type_info.DataSource = f_ds_rec_type.Tables[0];
                cbx_rec_type_info.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("收货类新提取失败!" + ee.ToString());
                return;
            }

            //初始化发货方式
            try
            {
                f_ds = main.g_class.comm_get_dictionary("send_type");
                DataRow drnew;
                drnew = f_ds.Tables[0].NewRow();
                drnew["send_type"] = "";
                f_ds.Tables[0].Rows.Add(drnew);
                cbx_send_type.ValueMember = "send_type";
                cbx_send_type.DisplayMember = "send_type";
                cbx_send_type.DataSource = f_ds.Tables[0];
                cbx_send_type.Text = "";

                DataSet f_ds_send_type = main.g_class.comm_get_dictionary("send_type");
                DataRow drnew_send_type;
                drnew_send_type = f_ds_send_type.Tables[0].NewRow();
                drnew_send_type["send_type"] = "";
                f_ds_send_type.Tables[0].Rows.Add(drnew_send_type);
                cbx_send_type_info.ValueMember = "send_type";
                cbx_send_type_info.DisplayMember = "send_type";
                cbx_send_type_info.DataSource = f_ds_send_type.Tables[0];
                cbx_send_type_info.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("发货方式提取失败!" + ee.ToString());
                return;
            }

            DataSet ds_supply;
            string str_sql = "";

            //初始化供应商
            try
            {
                if (main.dbtype == "SQL")
                {
                    str_sql = "select ltrim(rtrim(consignor)) consignor,ltrim(rtrim(consignor_name)) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and supply_flag = '1' and address4 = '1' order by consignor";
                    ds_supply = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    str_sql = "select trim(consignor) consignor,trim(consignor_name) consignor_name from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and supply_flag = '1' and address4 = '1' order by consignor";
                    ds_supply = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                DataRow drnew;
                DataSet ds_supplynew;

                drnew = ds_supply.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                ds_supply.Tables[0].Rows.Add(drnew);
                ds_supplynew = ds_supply.Copy();

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize customer's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            //DataTable dt_temp = new DataTable();
            //dt_temp.Columns.Add("consignor");
            //dt_temp.Columns.Add("send_no");
            //dt_temp.Columns.Add("id_no");
            //dt_temp.Columns.Add("arrive_time");
            //dgv_send_info.DataSource = dt_temp ;
            //查询语句
            string s_sql;
            s_sql = "select * from " + main.g_space + ".so_send_info where inv_org='" + main.g_org + "' and 1=2";

           
            DataSet ds = new DataSet();
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds = main.dbservice.Sql_exec_selectsql(s_sql);
                }
                else if (main.dbtype == "ORACLE")
                {
                    ds = main.dbservice.oracle_exec_selectsql(s_sql);
                }
                dgv_send_info.DataSource = ds.Tables[0];
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                
            }
         
        }

        //查询
        private void btn_Query_Click(object sender, EventArgs e)
        {
            ds_temp = new DataSet();
            ds_temp = f_get_so_send_info_header();

            //有数据
            //if (ds_temp.Tables[0].Rows.Count > 0)
            //{
            //    dgv_send_info.DataSource = ds_temp.Tables[0];
            //}
            ////无数据清空表
            //else
            //{
            //    if (dgv_send_info.Rows.Count > 0)
            //    {
            //        DataTable dt = dgv_send_info.DataSource as DataTable;
            //        DataTable dd = dgv_send_detail.DataSource as DataTable;
            //        dt.Rows.Clear();
            //        dd.Rows.Clear();
            //        dgv_send_info.DataSource = dt;
            //        dgv_send_detail.DataSource = dd;
            //    }
            //}

            if (dgv_send_info.Rows.Count > 0)
            {
                DataTable dt = dgv_send_info.DataSource as DataTable;
                DataTable dd = dgv_send_detail.DataSource as DataTable;
                dt.Rows.Clear();
                dd.Rows.Clear();
                dgv_send_info.DataSource = dt;
                dgv_send_detail.DataSource = dd;
            }
            if (ds_temp.Tables[0].Rows.Count > 0)
            {
                dgv_send_info.DataSource = ds_temp.Tables[0];
            }



            //说明有记录，默认第一条
            if (ds_temp.Tables[0].Rows.Count > 0)
            {
                f_display_info(ds_temp.Tables[0], 0);
            }
            else
            {
                f_clear_info();
            }
        }

        #region so_send_info表操作
        //新增操作
        private void btn_subnew_Click(object sender, EventArgs e)
        {
            //初始化明细
            f_clear_info();

            //界面状态修改
            i_dist = 1;

            //修改控件状态
            splitContainer1.Panel1.Enabled = false;
            panel_top.Enabled = false;
            f_panel_send_info_dist(true);
            btn_subnew.Enabled = false;
            btn_submodi.Enabled = false;
            btn_subdel.Enabled = false;

            btn_subsave.Enabled = true;
            btn_subcancel.Enabled = true;
        }

        //修改操作
        private void btn_submodi_Click(object sender, EventArgs e)
        {
            //如果有运货单
            if (dgv_send_info.CurrentRow != null)
            {
                //界面状态修改
                i_dist = 2;

                //修改控件状态
                splitContainer1.Panel1.Enabled = false;
                panel_top.Enabled = false;
                f_panel_send_info_dist(true);
                btn_subnew.Enabled = false;
                btn_submodi.Enabled = false;
                btn_subdel.Enabled = false;

                btn_subsave.Enabled = true;
                btn_subcancel.Enabled = true;

                //主键不能修改
                tb_send_no_info.Enabled = false;
            }
        }

        //删除操作
        private void btn_subdel_Click(object sender, EventArgs e)
        {
            //如果有运货单
            if (dgv_send_info.CurrentRow != null)
            {
                //提示是否删除
                if (MessageBox.Show("是否删除?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string s_sql = "update " + main.g_space + ".so_send_info set main_dist = 'D' where inv_org='" + main.g_org + "' and id_no='" + tb_id_no_info.Text + "' and send_no='" + tb_send_no_info.Text + "'";
                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_exec_commandsql(s_sql);
                        }
                        else if (main.dbtype == "ORACLE")
                        {
                            main.dbservice.oracle_exec_commandsql(s_sql);
                        }
                    }
                    catch (System.Exception ee)
                    {
                        MessageBox.Show("删除失败!" + ee.ToString());
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                    if (dgv_send_detail.Rows.Count > 0)
                    {
                        string i_asn_no="";
                        for (int i = 0; i < dgv_send_detail.Rows.Count; i++)
                        {
                            i_asn_no += "'"+dgv_send_detail.Rows[i].Cells["send_detail_asn_no"].Value.ToString().Trim()+"',";
                        }
                        i_asn_no = i_asn_no.Substring(0, i_asn_no.Length-1);
                        string s_sql_string = "update " + main.g_space + ".so_send_detail set main_dist ='D' where inv_org='" + main.g_org + "' and id_no='" + tb_id_no_info.Text + "' and asn_no in (" + i_asn_no + ")";
                        try
                        {
                            if (main.dbtype == "SQL")
                            {
                                main.dbservice.Sql_exec_commandsql(s_sql_string);
                            }
                            else if (main.dbtype == "ORACLE")
                            {
                                main.dbservice.oracle_exec_commandsql(s_sql);
                            }
                        }
                        catch (System.Exception ee)
                        {
                            MessageBox.Show("删除失败!" + ee.ToString());
                            Cursor.Current = Cursors.Default;
                            return;
                        }
                    
                    }
                    MessageBox.Show("删除成功!请继续!");
                    i_dist = 3;
                    //刷新界面
                    btn_Query_Click(sender, e);

                    
                }
            }
        }

        //保存操作
        private void btn_subsave_Click(object sender, EventArgs e)
        {
            string s_sql = "",str_idno="";
            //判断货主知否合法
            if (tb_newcusname.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Consignor is not exists or haven't privilege", main.g_info_dataset) + "!");
                return;
            }
            if (cbx_newcustomer.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please choose a consignor", main.g_info_dataset) + "!");
                return;
            }
            //新增保存
            if (i_dist == 1)
            {
                str_idno = DateTime.Now.ToString("yyyyMMddHHmmss");
                //判断主键空值
                if (string.IsNullOrEmpty(tb_send_no_info.Text))
                {
                    MessageBox.Show("运单号不能为空");
                    return;
                }
                if (string.IsNullOrEmpty(tb_quantity.Text))
                {
                    tb_quantity.Text = "0";
                }
                if (string.IsNullOrEmpty(tb_net_weight.Text))
                {
                    tb_net_weight.Text = "0";
                }
                s_sql = "insert into " + main.g_space + ".so_send_info( inv_org, consignor,          id_no," +
                                                                         "send_no,           arrive_time,        sku_item," +
                                                                         "rec_type,           quantity,          net_weight," +
                                                                         "send_name,          send_type," +
                                                                         "unload_time_s,     unload_time_e,      unload_signer," +
                                                                         "check_time_s,      check_time_e,       check_memo,     check_signer," +
                                                                         "sap_rec_s,         sap_rec_e,          sap_signer," +
                                                                         "rec_pack_s,        rec_pack_e,         rec_pack_signer," +
                                                                         "putaway_time_s,    putaway_time_e,     putaway_signer," +
                                                                         "confirm_time_s,    confirm_time_e,     confirm_signer, memo," +
                                                                         "main_dist,         made_time,          maker)";

                if (main.dbtype == "SQL")
                {
                    s_sql += "values('" + main.g_org + "','"+cbx_newcustomer.Text.ToString().Trim()+"','" + str_idno + "'," +
"'" + tb_send_no_info.Text + "','" + dtp_arrive_time.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',isnull('" + tb_sku_item.Text + "',0)," +
"'" + cbx_rec_type_info.Text + "',isnull('" + tb_quantity.Text + "',0),isnull('" + tb_net_weight.Text + "',0)," +
"'" + tb_send_name_info.Text + "','" + cbx_send_type_info.Text + "'," +
"'" + dtp_unload_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + dtp_unload_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + tb_unload_signer.Text + "'," +
"'" + dtp_check_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + dtp_check_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + tb_check_memo.Text + "','" + tb_check_signer.Text + "'," +
"'" + dtp_sap_rec_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + dtp_sap_rec_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + tb_sap_signer.Text + "'," +
"'" + dtp_rec_pack_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + dtp_rec_pack_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + tb_rec_pack_signer.Text + "'," +
"'" + dtp_putaway_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + dtp_putaway_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + tb_putaway_signer.Text + "'," +
"'" + dtp_confirm_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + dtp_confirm_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','" + tb_confirm_signer.Text + "','" + tb_memo.Text + "'," +
"'A',getdate(),'" + main.g_user + "')";
                }
                else if (main.dbtype == "ORACLE")
                {
                    s_sql += "values('" + main.g_org + "','"+cbx_newcustomer.Text.ToString().Trim()+"','" + str_idno + "'," +
"'" + tb_send_no_info.Text + "',to_date('" + dtp_arrive_time.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_sku_item.Text +
"','" + cbx_rec_type_info.Text + "','" + tb_quantity.Text + "'," + "'" + tb_net_weight.Text +
"','" + tb_send_name_info.Text + "','" + cbx_send_type_info.Text + "'," +
"to_date('" + dtp_unload_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),to_date('" + dtp_unload_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_unload_signer.Text + "'," +
"to_date('" + dtp_check_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),to_date('" + dtp_check_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_check_memo.Text + "','" + tb_check_signer.Text + "'," +
"to_date('" + dtp_sap_rec_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),to_date('" + dtp_sap_rec_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_sap_signer.Text + "'," +
"to_date('" + dtp_rec_pack_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),to_date('" + dtp_rec_pack_e.Value.ToString("yyyy.MM.dd") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_rec_pack_signer.Text + "'," +
"to_date('" + dtp_putaway_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),to_date('" + dtp_putaway_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_putaway_signer.Text + "'," +
"to_date('" + dtp_confirm_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),to_date('" + dtp_confirm_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),'" + tb_confirm_signer.Text + "','" + tb_memo.Text + "'," +
"'A',sysdate,'" + main.g_user + "')";
                }


            }
            //修改保存
            else if (i_dist == 2)
            {
                if (main.dbtype == "SQL")
                {
                    s_sql = "update " + main.g_space + ".so_send_info set arrive_time='" + dtp_arrive_time.Value.ToString("yyyy.MM.dd HH:mm:ss") + "'" +
                            ",sku_item='" + tb_sku_item.Text + "',rec_type='" + cbx_rec_type_info.Text + "',quantity='" + tb_quantity.Text + "'," +
                            "net_weight='" + tb_net_weight.Text + "',send_name='" + tb_send_name_info.Text + "',send_type='" + cbx_send_type_info.Text + "'," +
                            "unload_time_s='" + dtp_unload_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',unload_time_e='" + dtp_unload_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',unload_signer='" + tb_unload_signer.Text + "'," +
                            "check_time_s='" + dtp_check_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',check_time_e='" + dtp_check_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',check_memo='" + tb_check_memo.Text + "',check_signer='" + tb_check_signer.Text + "'," +
                            "sap_rec_s='" + dtp_sap_rec_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',sap_rec_e='" + dtp_sap_rec_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',sap_signer='" + tb_sap_signer.Text + "'," +
                            "rec_pack_s='" + dtp_rec_pack_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',rec_pack_e='" + dtp_rec_pack_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',rec_pack_signer='" + tb_rec_pack_signer.Text + "'," +
                            "putaway_time_s='" + dtp_putaway_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',putaway_time_e='" + dtp_putaway_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',putaway_signer='" + tb_putaway_signer.Text + "'," +
                            "confirm_time_s='" + dtp_confirm_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',confirm_time_e='" + dtp_confirm_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "',confirm_signer='" + tb_confirm_signer.Text + "', memo='" + tb_memo.Text + "'," +
                            "main_dist='C',update_time=getdate(),updater='" + main.g_user + "' " +
                            "where inv_org='"+main.g_org+"' and id_no='" + tb_id_no_info.Text + "' and send_no='" + tb_send_no_info.Text + "'";
                }
                else if (main.dbtype == "ORACLE")
                {

                    s_sql = "update " + main.g_space + ".so_send_info set arrive_time=to_date('" + dtp_arrive_time.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss')," +
                            "sku_item='" + tb_sku_item.Text + "',rec_type='" + cbx_rec_type_info.Text + "',quantity='" + tb_quantity.Text + "'," +
                            "net_weight='" + tb_net_weight.Text + "',send_name='" + tb_send_name_info.Text + "',send_type='" + cbx_send_type_info.Text + "'," +
                            "unload_time_s=to_date('" + dtp_unload_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),unload_time_e=to_date('" + dtp_unload_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),unload_signer='" + tb_unload_signer.Text + "'," +
                            "check_time_s=to_date('" + dtp_check_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),check_time_e=to_date('" + dtp_check_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),check_memo='" + tb_check_memo.Text + "',check_signer='" + tb_check_signer.Text + "'," +
                            "sap_rec_s=to_date('" + dtp_sap_rec_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),sap_rec_e=to_date('" + dtp_sap_rec_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),sap_signer='" + tb_sap_signer.Text + "'," +
                            "rec_pack_s=to_date('" + dtp_rec_pack_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),rec_pack_e=to_date('" + dtp_rec_pack_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),rec_pack_signer='" + tb_rec_pack_signer.Text + "'," +
                            "putaway_time_s=to_date('" + dtp_putaway_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),putaway_time_e=to_date('" + dtp_putaway_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd'),putaway_signer='" + tb_putaway_signer.Text + "'," +
                            "confirm_time_s=to_date('" + dtp_confirm_time_s.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),confirm_time_e=to_date('" + dtp_confirm_time_e.Value.ToString("yyyy.MM.dd HH:mm:ss") + "','yyyy.mm.dd HH:mm:ss'),confirm_signer='" + tb_confirm_signer.Text + "', memo='" + tb_memo.Text + "'," +
                            "main_dist='C',update_time=sysdate,updater='" + main.g_user + "' " +
                            "where inv_org='"+main.g_org+"' and id_no='" + tb_id_no_info.Text + "' and send_no='" + tb_send_no_info.Text + "'";
                }
            }
            try
            {
                if (main.dbtype == "SQL")
                {
                    main.dbservice.Sql_exec_commandsql(s_sql);
                }
                else if (main.dbtype == "ORACLE")
                {
                    main.dbservice.oracle_exec_commandsql(s_sql);
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("保存失败!" + ee.ToString());
                Cursor.Current = Cursors.Default;
                return;
            }

            //MessageBox.Show("保存成功!请继续!");
            //修改界面状态
           

            //修改控件状态
            splitContainer1.Panel1.Enabled = true;
            panel_top.Enabled = true;
            f_panel_send_info_dist(false);
            btn_subnew.Enabled = true;
            btn_submodi.Enabled = true;
            btn_subdel.Enabled = true;

            btn_subsave.Enabled = false;
            btn_subcancel.Enabled = false;

            //主键状态修改

            //cbx_newcustomer.Enabled = true;
            tb_id_no_info.Enabled = true;
            tb_send_no_info.Enabled = true;
            if (i_dist == 1)
            {
                //如果是新增，就增加一行，并且选中
                DataRow newdr = ((DataTable)dgv_send_info.DataSource).NewRow();
                newdr["consignor"] = "";
                newdr["send_no"] = tb_send_no_info.Text.Trim();
                newdr["id_no"] = str_idno;
                newdr["arrive_time"] = dtp_arrive_time.Value.ToString("yyyy.MM.dd HH:mm:ss");
                b_temp = false;
                ((DataTable)dgv_send_info.DataSource).Rows.Add(newdr);
                for (int i = 0; i < dgv_send_info.Rows.Count; i++)
                {
                    dgv_send_info.Rows[i].Selected = false;
                }
                dgv_send_info.Rows[dgv_send_info.Rows.Count - 1].Selected = true;
                tb_id_no_info.Text = str_idno;
                b_temp = true;
            }
            i_dist = 0;
            //刷新界面
           // btn_Query_Click(sender, e);
        }

        //取消操作
        private void btn_subcancel_Click(object sender, EventArgs e)
        {
            //如果有数据
            if (dgv_send_info.CurrentRow != null)
            {
                f_display_info(ds_temp.Tables[0], dgv_send_info.CurrentRow.Index);

                //修改控件状态
                splitContainer1.Panel1.Enabled = true;
                panel_top.Enabled = true;
                f_panel_send_info_dist(false);

                btn_subnew.Enabled = true;
                btn_submodi.Enabled = true;
                btn_subdel.Enabled = true;

                btn_subsave.Enabled = false;
                btn_subcancel.Enabled = false;

                //主键状态修改

                //cbx_newcustomer.Enabled = true;
                tb_id_no_info.Enabled = true;
                tb_send_no_info.Enabled = true;

                i_dist = 0;
            }
            else
            {
                //修改控件状态
                splitContainer1.Panel1.Enabled = true;
                panel_top.Enabled = true;
                f_panel_send_info_dist(false);

                btn_subnew.Enabled = true;
                btn_submodi.Enabled = true;
                btn_subdel.Enabled = true;

                btn_subsave.Enabled = false;
                btn_subcancel.Enabled = false;

                //主键状态修改

                //cbx_newcustomer.Enabled = true;
                tb_id_no_info.Enabled = true;
                tb_send_no_info.Enabled = true;

                i_dist = 0;
            
            }
        }

        //品种数量输入控制
        private void tb_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                if (e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }
        }

        //数量重量输入控制
        private void tb_numeric_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                if (e.KeyChar != '\b' && e.KeyChar != '.')
                {
                    e.Handled = true;
                }
            }
        }
        #endregion

        //表头选项发生变化
        public string g_consignor, g_id_no, g_send_no;
        private void dgv_send_info_SelectionChanged(object sender, EventArgs e)
        {
            //如果有数据
            if (dgv_send_info.CurrentRow != null && b_temp==true)
            {

                if (ds_temp.Tables.Count>0)
                {
                    f_display_info(ds_temp.Tables[0], dgv_send_info.CurrentRow.Index);
                }
                f_Bind_so_send_detail(dgv_send_info.CurrentRow.Cells["consignor"].Value.ToString().Trim(),
                                      dgv_send_info.CurrentRow.Cells["id_no"].Value.ToString().Trim(),
                                      dgv_send_info.CurrentRow.Cells["send_no"].Value.ToString().Trim());
                i_rows_count = dgv_send_detail.Rows.Count;
                g_consignor = dgv_send_info.CurrentRow.Cells["consignor"].Value.ToString().Trim();
                g_id_no=dgv_send_info.CurrentRow.Cells["id_no"].Value.ToString().Trim();
                g_send_no = dgv_send_info.CurrentRow.Cells["send_no"].Value.ToString().Trim();
            }
        }


        #region so_send_detail表操作
        //dgv_send_detail新增
        private void btn_send_detail_add_Click(object sender, EventArgs e)
        {
            //有数据
            if (dgv_send_info.CurrentRow != null)
            {
                //新增一行
                ((DataTable)dgv_send_detail.DataSource).Rows.Add();
                //带出客户和单号
                dgv_send_detail.Rows[dgv_send_detail.Rows.Count - 1].Cells["send_detail_inv_org"].Value = main.g_org;
                dgv_send_detail.Rows[dgv_send_detail.Rows.Count - 1].Cells["send_detail_consignor"].Value = dgv_send_info.CurrentRow.Cells["consignor"].Value;
                dgv_send_detail.Rows[dgv_send_detail.Rows.Count - 1].Cells["send_detail_id_no"].Value = dgv_send_info.CurrentRow.Cells["id_no"].Value;
                dgv_send_detail.Rows[dgv_send_detail.Rows.Count - 1].Cells["send_detail_send_no"].Value = dgv_send_info.CurrentRow.Cells["send_no"].Value;
                dgv_send_detail.Rows[dgv_send_detail.Rows.Count - 1].Selected = true;

                //光标显示在新增箱子列
                int rows = dgv_send_detail.Rows.Count - 1;
                dgv_send_detail.CurrentCell = dgv_send_detail["send_detail_customer_order_no", rows];
                dgv_send_detail.BeginEdit(true);

                //界面状态修改
                i_dist = 1;

                //修改控件状态
                splitContainer1.Panel1.Enabled = false;
                panel_top.Enabled = false;
                //f_panel_send_info_dist(true);

                btn_send_detail_add.Enabled = false;
                btn_send_detail_modi.Enabled = false;
                btn_send_detail_del.Enabled = false;

                btn_send_detail_save.Enabled = true;
                btn_send_detail_cancel.Enabled = true;
            }
        }


        //dgv_send_detail修改
        private void btn_send_detail_modi_Click(object sender, EventArgs e)
        {
            //如果有数据
            if (dgv_send_detail.CurrentRow != null)
            {
                //界面状态修改
                i_dist = 2;

                //修改控件状态
                splitContainer1.Panel1.Enabled = false;
                panel_top.Enabled = false;
                f_panel_send_info_dist(false);

                btn_send_detail_add.Enabled = false;
                btn_send_detail_modi.Enabled = false;
                btn_send_detail_del.Enabled = false;

                btn_send_detail_save.Enabled = true;
                btn_send_detail_cancel.Enabled = true;
            }
        }

        //dgv_send_detail保存
        private void btn_send_detail_save_Click(object sender, EventArgs e)
        {
            //新增
            if (i_dist == 1)
            {
                for (int i = 0; i < dgv_send_detail.Rows.Count; i++)
                {
                    if (string.IsNullOrEmpty(dgv_send_detail.Rows[i].Cells["send_detail_customer_order_no"].Value.ToString().Trim()))
                    {
                        MessageBox.Show("客户订单号不能为空!");
                        return;
                    }
                }
            }

            try
            {
                if (main.dbtype == "SQL")
                {
                    main.dbservice.Sql_refresh_db(i_ds_send_detail, i_sql_send_detail);
                }
                else if (main.dbtype == "ORACLE")
                {
                    main.dbservice.oracle_refresh_db(i_ds_send_detail, i_sql_send_detail);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to save box", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }

            i_dist = 0;

            //修改控件状态
            splitContainer1.Panel1.Enabled = true;
            panel_top.Enabled = true;
            f_panel_send_info_dist(false);

            btn_send_detail_add.Enabled = true;
            btn_send_detail_modi.Enabled = true;
            btn_send_detail_del.Enabled = true;

            btn_send_detail_save.Enabled = false;
            btn_send_detail_cancel.Enabled = false;

            f_Bind_so_send_detail(dgv_send_info.CurrentRow.Cells["consignor"].Value.ToString().Trim(),
                                      dgv_send_info.CurrentRow.Cells["id_no"].Value.ToString().Trim(),
                                      dgv_send_info.CurrentRow.Cells["send_no"].Value.ToString().Trim());
        }

        //dgv_send_detail
        private void btn_send_detail_cancel_Click(object sender, EventArgs e)
        {
            i_dist = 0;

            //修改控件状态
            splitContainer1.Panel1.Enabled = true;
            panel_top.Enabled = true;
            //f_panel_send_info_dist(true);

            btn_send_detail_add.Enabled = true;
            btn_send_detail_modi.Enabled = true;
            btn_send_detail_del.Enabled = true;

            btn_send_detail_save.Enabled = false;
            btn_send_detail_cancel.Enabled = false;

            //刷新
            dgv_send_info_SelectionChanged(sender, e);
            
        }

        //dgv_send_detail删除
        private void btn_send_detail_del_Click(object sender, EventArgs e)
        {
            //有数据
            if (dgv_send_detail.CurrentRow != null)
            {
                //提示是否删除
                if (MessageBox.Show("是否删除?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string s_sql = "delete " + main.g_space + ".so_send_detail where inv_org='" + main.g_org +
                        "' and asn_no='" + dgv_send_detail.CurrentRow.Cells["send_detail_asn_no"].Value.ToString() +
                        "' and id_no='" + dgv_send_detail.CurrentRow.Cells["send_detail_id_no"].Value.ToString() +
                        "' and send_no='" + dgv_send_detail.CurrentRow.Cells["send_detail_send_no"].Value.ToString() +
                        "' and customer_order_no='" + dgv_send_detail.CurrentRow.Cells["send_detail_customer_order_no"].Value.ToString() + "'";

                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_exec_commandsql(s_sql);
                        }
                        else if (main.dbtype == "ORACLE")
                        {
                            main.dbservice.oracle_exec_commandsql(s_sql);
                        }
                    }
                    catch (System.Exception ee)
                    {
                        MessageBox.Show("删除失败!" + ee.ToString());
                        Cursor.Current = Cursors.Default;
                        return;
                    }

                    MessageBox.Show("删除成功!请继续!");
                    i_ds_send_detail.Tables[0].Rows.RemoveAt(dgv_send_detail.CurrentRow.Index);
                    dgv_send_detail.DataSource = i_ds_send_detail.Tables[0];
                }
            }
        }

        #endregion

        private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            //操作运单明细不予变更选项卡
            if (i_dist > 0)
            {
                //界面状态修改  
                i_dist = 2;

                //修改控件状态
                splitContainer1.Panel1.Enabled = false;
                panel_top.Enabled = false;
                f_panel_send_info_dist(true);

                btn_subnew.Enabled = false;
                btn_submodi.Enabled = false;
                btn_subdel.Enabled = false;

                btn_subsave.Enabled = true;
                btn_subcancel.Enabled = true;

                e.Cancel = true;
            }
        }

        private void dgv_send_detail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_send_detail.CurrentRow == null)
            {
                return;
            }
            //说明是客户订单号列
            if (e.ColumnIndex == 0)
            {
                if (!string.IsNullOrEmpty(dgv_send_detail.Rows[e.RowIndex].Cells["send_detail_customer_order_no"].Value.ToString()))
                {
                    //找出asn_no
                    //查询语句
                    string s_sql;
                    s_sql = "select asn_no from " + main.g_space + ".asn_order_header where inv_org='" + main.g_org + "' and consignor='" + dgv_send_detail.Rows[e.RowIndex].Cells["send_detail_consignor"].Value.ToString() +
                            "' and customer_order_no='" + dgv_send_detail.Rows[e.RowIndex].Cells["send_detail_customer_order_no"].Value.ToString() + "' and asn_status='0'";
                    DataSet ds = new DataSet();
                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            ds = main.dbservice.Sql_exec_selectsql(s_sql);
                        }
                        else if (main.dbtype == "ORACLE")
                        {
                            ds = main.dbservice.oracle_exec_selectsql(s_sql);
                        }

                        //有数据
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            dgv_send_detail.Rows[e.RowIndex].Cells["send_detail_asn_no"].Value = ds.Tables[0].Rows[0][0].ToString();
                        }
                        else
                        {
                            MessageBox.Show("客户订单没有相应的入库信息!");
                            dgv_send_detail.Rows[e.RowIndex].Cells["send_detail_asn_no"].Value = "";
                        }
                    }
                    catch (System.Exception ee)
                    {
                        MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());
                        return;
                    }
                }
            }
        }

        private void dgv_send_detail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //编辑控制
            //新增
            if (i_dist == 1)
            {
                //如果是新增行
                if (dgv_send_detail.CurrentRow.Index > i_rows_count - 1)
                {
                    dgv_send_detail.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
                }
                else
                {
                    dgv_send_detail.EditMode = DataGridViewEditMode.EditProgrammatically;
                }
            }
            //修改
            else if (i_dist == 2)
            {
                dgv_send_detail.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
            }
            else
            {
                //设置不可编辑
                dgv_send_detail.EditMode = DataGridViewEditMode.EditProgrammatically;
            }
        }
 
        private void dgv_send_detail_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (btn_send_detail_add.Enabled == false||btn_send_detail_modi.Enabled == false)
            {
                w_asn998_order su = new w_asn998_order(g_consignor, g_id_no, g_send_no);
                su.Owner = this;
                su.ShowDialog();

                i_dist = 0;

                //修改控件状态
                splitContainer1.Panel1.Enabled = true;
                panel_top.Enabled = true;
                //f_panel_send_info_dist(true);

                btn_send_detail_add.Enabled = true;
                btn_send_detail_modi.Enabled = true;
                btn_send_detail_del.Enabled = true;

                btn_send_detail_save.Enabled = false;
                btn_send_detail_cancel.Enabled = false;

                //刷新
                dgv_send_info_SelectionChanged(sender, e);
            }
        }

        private void btn_print_Click(object sender, EventArgs e)
        {
            if ((tb_id_no_info.Text.ToString().Trim() != "") && (tb_send_no_info.Text.ToString().Trim() != ""))
            {
                DataSet ds_print = new DataSet();
                ds_print.Tables.Add("0");
                ds_print.Tables[0].Columns.Add("send_no", System.Type.GetType("System.String"));
                ds_print.Tables[0].Columns.Add("arrive_time", System.Type.GetType("System.String"));
                ds_print.Tables[0].Rows.Add("0");
                ds_print.Tables[0].Rows[0]["send_no"] = tb_send_no_info.Text.ToString().Trim();
                ds_print.Tables[0].Rows[0]["arrive_time"] = dtp_arrive_time.Text.ToString().Trim();

                ds_print.Tables[0].Columns.Add("id_no", System.Type.GetType("System.Byte[]"));

                b_bar.IncludeLabel = true;
                b_bar.Encode(BarcodeLib.TYPE.CODE128, tb_id_no_info.Text.ToString().Trim(), System.Drawing.Color.Black, System.Drawing.Color.White, 420, 60);
                byte[] byteArray = b_bar.GetImageData(BarcodeLib.SaveTypes.PNG);

                ds_print.Tables[0].Rows[0]["id_no"] = byteArray;//将图片存储到dataSet中 

                try
                {
                    this.asn998_rpt1.SetDataSource(ds_print.Tables[0]);
                    this.crystalReportViewer1.ReportSource = asn998_rpt1;                    
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                    return;
                }
                this.crystalReportViewer1.PrintReport();
                
            }
        }

        private void tb_id_no_info_TextChanged(object sender, EventArgs e)
        {
            if (tb_id_no_info.Text.ToString().Trim() != "")
            {
                btn_print.Enabled = true;
            }
            else
            {
                btn_print.Enabled = false;
            }
        }

        private void cbx_customer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_customer.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void cbx_newcustomer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_newcustomer.Text.Trim();
            tb_newcusname.Text = main.g_class.comm_findname(str_cust, newdscustomer);
        }

        private void cbx_newcustomer_TextChanged_1(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_newcustomer.Text.Trim();
            tb_newcusname.Text = main.g_class.comm_findname(str_cust, newdscustomer);
        }
    }
}